<?php
    $result = mysqli_connect('localhost','root','root');
    if(!$result){
        echo "数据库连接失败";
    }
    mysqli_query($result,"set names 'utf8'");
    $select_db = mysqli_select_db($result,"lam");
    if(!$select_db){
        echo "不能连接到此数据库:\n".mysqli_error();
    }
    
    if(!empty($_POST['submit'])){
        $txtName = $_POST['txtName'];
        $txtMessage = $_POST['txtMessage'];
        $sql = "insert into guestbook(txtName,txtMessage,addtime) values('".$txtName."','".$txtMessage."',".time().")";
    }
    $re = mysqli_query($result,$sql);
    if($re){
        echo "<script>alert('留言成功！');location.href='contact.php';</script>";
    }else{
        echo "<script>alert('留言失败！请重试');location.href='contact.php';</script>";
    }
?>